package com.example.springboot_code_read.conversionService;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;

public class ConversionServiceTest {

    public static void main(String[] args) {

        ConversionService conversionService = new DefaultConversionService();

        if (conversionService.canConvert(Integer.class, String.class)) {

            System.out.println("Can convert from Integer to String");

            String convert = conversionService.convert(666, String.class);
            System.out.println("Converted: " + convert);

        }

        // 使用 TypeDescriptor 检查转换是否可能
        if (conversionService.canConvert(
                TypeDescriptor.valueOf(Integer.class),
                TypeDescriptor.valueOf(String.class))) {
            System.out.println("Can convert from Integer to String using TypeDescriptors");

            // 使用 TypeDescriptor 执行转换
            Object convertedWithTypeDescriptor = conversionService.convert(
                    888,
                    TypeDescriptor.valueOf(Integer.class),
                    TypeDescriptor.valueOf(String.class));
            System.out.println("Converted with TypeDescriptors: " + convertedWithTypeDescriptor);
        }

    }

}
